package com.zjj;

import com.zjj.pojo.Student;
import com.zjj.utils.MyBatisUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.List;
import java.util.Scanner;

public class StudentMain {
    private static SqlSession session = MyBatisUtils.getSession();;

    public static void main(String[] args) {
        printMenu();
        Scanner sc = new Scanner(System.in);
        int choice = sc.nextInt();
        while (choice != 0 || choice < 0 || choice > 3) {
            if (choice == 1) {
                System.out.println("多条件查询");
                employeeFindByIdTest();
            } else if (choice == 2) {
                System.out.println("单条件查询");
                employeeFindByidTest();
            } else if (choice == 0) {
                System.out.println("退出系统");
            } else {
                System.out.println("输入错误，请重新输入");
            }
            printMenu();
            choice = sc.nextInt();

        }
        System.out.println("欢迎下次光临");
        session.commit();
        session.close();

    }

    private static void employeeFindByidTest() {
        System.out.print("请输入学生编号：");
        String sno = new Scanner(System.in).nextLine();
        int i = sno.compareTo("");
        sno = sno.equals("")? "0" : sno;
        Integer snoInt = Integer.parseInt(sno);
        Student student = new Student();
        student.setSno(snoInt);
        List<Object> list = session.selectList("selectBySnoLessThan", student);
        if (!list.isEmpty()) {
            if (snoInt == 0) {
                System.out.println("查询成功！,查询所有学生结果如下：");
                for (Object object : list) {
                    System.out.println(object);
                }
            }else {
                System.out.println("查询成功！,查询id小于" + sno + "的学生结果如下：");
                for (Object object : list) {
                    System.out.println(object);
                }
            }
        }else{
            System.out.println("查询条件不正确，查询失败！");
        }

    }

    private static void employeeFindByIdTest() {

        System.out.print("请输入学生姓名：");
        String name = new Scanner(System.in).nextLine();
        System.out.print("请输入学生专业：");
        String major = new Scanner(System.in).nextLine();
        Student student = new Student(name, major);
        List<Object> list = session.selectList("selectBySnameAndMajorAndSnoIsNull", student);
        if (!list.isEmpty()) {
            System.out.println("查询成功！");
            for (Object object : list) {
                System.out.println(object);
            }
        }else{
            System.out.println("查询条件不正确，查询失败！");
        }
    }




    //菜单
    private static void printMenu(){
        System.out.println("==欢迎使用学生信息查询系统==");
        System.out.println("1.多条件查询");
        System.out.println("2.单条件查询");
        System.out.println("0.退出系统");
        System.out.print("请输入选项：");
    }

}
